package com.lishilin.gulimall.product.app;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lishilin.gulimall.product.dto.AttrDto;
import com.lishilin.gulimall.product.entity.AttrEntity;
import com.lishilin.gulimall.product.service.AttrAttrgroupRelationService;
import com.lishilin.gulimall.product.service.AttrService;
import com.lishilin.gulimall.product.service.CategoryService;
import com.lishilin.gulimall.product.vo.AttrGroupRelationVo;
import com.lishilin.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lishilin.gulimall.product.entity.AttrGroupEntity;
import com.lishilin.gulimall.product.service.AttrGroupService;
import com.lishilin.common.utils.PageUtils;
import com.lishilin.common.utils.R;


/**
 * 属性分组
 *
 * @author lishilin
 * @email 1871907207@qq.com
 * @date 2021-04-19 16:35:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> params) {
        relationService.saveBatch(params);
        return R.ok();
    }

    /**
     * 根据分类id查出所有的分组以及这些分组下所有的属性
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    ;

    //批量删除 属性分组关联表中的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 根据分组id从属性表中查询他没有关联的属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")//路径变量 要标注@PathVariable("catelogId") 取出catelogId的值
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);

    }

    /**
     * 根据分组id从属性表中查询他所关联的属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")//路径变量 要标注@PathVariable("catelogId") 取出catelogId的值
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entityList = attrService.getRelationAttr(attrgroupId);

        if (entityList != null) {
            List<AttrDto> attrDtos = new ArrayList<>();
            for (AttrEntity attrEntity : entityList) {
                AttrDto attrDto = new AttrDto();
                BeanUtils.copyProperties(attrEntity, attrDto);
                attrDtos.add(attrDto);
            }
            return R.ok().put("data", attrDtos);
        } else {
            return R.ok().put("data", new ArrayList<>());
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")//路径变量 要标注@PathVariable("catelogId") 取出catelogId的值
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);

        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
